package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.CrmSynAdapter;
import com.jianyun.jyzs.bean.CrmListResponse;
import com.jianyun.jyzs.dao.CrmDao;
import com.jianyun.jyzs.http.CrmSynachroHelper;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.widget.CrmSearchPopu;
import com.jianyun.jyzs.widget.LoadingDialog;
import com.jrmf360.rylib.common.util.ToastUtil;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by yangshijun on 2018/6/4.
 */

public class CrmSynchroActivity extends AppCompatActivity implements CrmSynachroHelper.CrmSyncListener, SwipyRefreshLayout.OnRefreshListener {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.tv_checked)
    public TextView tvChecked;
    @BindView(R.id.tv_allChecked)
    TextView tvAllChecked;
    @BindView(R.id.topClander)
    ImageView topClander;
    @BindView(R.id.iv_app_moreLinearLayout)
    LinearLayout ivAppMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.loadButton)
    Button loadButton;
    @BindView(R.id.nullLinearLayout)
    LinearLayout nullLinearLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipy)
    SwipyRefreshLayout swipyRefreshLayout;

    private String userId;
    private int page = 1;
    private List<CrmListResponse.CrmListBean> crmList;
    private CrmSynAdapter crmSynAdapter;
    private CrmDao crmDao;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_crm_synchro);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        initView();
       // CrmSynachroHelper.getCrmList(userId, page, this);
    }

    private void initView() {
        loadingDialog = LoadingDialog
                .create(this)
                .setDetailLabel("正在同步数据");
        loadingDialog.setCancellable(true);
        crmDao = new CrmDao(this);
        crmList = new ArrayList<>();
        topBack.setVisibility(View.VISIBLE);
        topText.setVisibility(View.VISIBLE);
        tvChecked.setVisibility(View.VISIBLE);
        tvAllChecked.setVisibility(View.VISIBLE);
        ivAppMoreLinearLayout.setVisibility(View.VISIBLE);
        ivMore.setVisibility(View.VISIBLE);
        topText.setText("CRM");
        tvChecked.setText("同步");
        swipyRefreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        crmSynAdapter = new CrmSynAdapter(this, crmList);
        recyclerView.setAdapter(crmSynAdapter);
    }

    @OnClick({R.id.topBack, R.id.iv_more, R.id.tv_checked, R.id.tv_allChecked})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.iv_more:
                CrmSearchPopu searchPopu = new CrmSearchPopu(this);
                searchPopu.show(view);
                break;
            case R.id.tv_checked:
                List<CrmListResponse.CrmListBean> selectedList = crmSynAdapter.getSelectedList();

                if (selectedList.size() > 0) {
                    loadingDialog.show();
                    CrmSynachroHelper.getCrmDetial(crmDao, selectedList, userId,this);
                } else {
                    ToastUtil.showNoWaitToast(this, "还未选中任何数据");
                }
                break;
            case R.id.tv_allChecked:
                crmSynAdapter.setCheckAll();
                break;
        }
    }


    //上拉加载更多
    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
        //CrmSynachroHelper.getCrmList(userId, page += 1,this);
    }

    //根据条件搜索
    public void search(String searchtitle, String searchstime, String searchetime) {
//        ToastUtil.showNoWaitToast(ac,"title="+searchtitle+"startTime="+searchstime+"endTime="+searchetime);
       // CrmSynachroHelper.searchCrmList(userId, searchtitle, searchstime, searchetime,this);
    }

    @Override
    public void getCrmListSuccess(List<CrmListResponse.CrmListBean> list) {
        if (list.size() > 0) {
            crmList.addAll(list);
            crmSynAdapter.setList(crmList);
        } else {
            swipyRefreshLayout.setRefreshing(false);
            swipyRefreshLayout.setEnabled(false);
            if (page != 1) ToastUtil.showNoWaitToast(this, "没有跟多数据");
        }
    }

    @Override
    public void getCrmlistFail(String msg) {
        ToastUtil.showNoWaitToast(this, msg);
    }

    @Override
    public void synchroOver(List<CrmListResponse.CrmListBean> removeindex) {
        crmList.removeAll(removeindex);
        crmSynAdapter.setList(crmList);
        loadingDialog.dismiss();
        ToastUtil.showNoWaitToast(this, "同步成功");
    }

    @Override
    public void searchCrm(List<CrmListResponse.CrmListBean> list) {
        if (list != null) {
            crmList.clear();
            crmList.addAll(list);
            crmSynAdapter.setList(crmList);
        } else {
            ToastUtil.showNoWaitToast(this, "没有结果");
        }
    }

}
